let timer;

export const useSms = (time = 60) => {
  const isSending = ref(false);
  const timeRef = ref(time);

  const send = async (content: { phone: string; smsType: string }) => {
    const { phone, smsType } = content;
    const api = '/user/register/send-verify-code'
    const res = await post(Prefix.selection + api, {
      phone,
      smsType,
    });
    if (res.code == 'ok') {
      isSending.value = true;
      if (timer) clearInterval(timer);
      timer = setInterval(() => {
        if (timeRef.value > 0) {
          timeRef.value -= 1;
        } else {
          clearInterval(timer);
          timer = null;
          isSending.value = false;
        }
      }, 1000);
    } else {
      // message.error(res.message);
    }
  };

  return {
    isSending,
    send,
    timeRef,
  };
};
